package com.example;

import java.util.Random;

/**
 * @author Jason
 * @PACKAGE_NAME com.example
 * @Description 格斗实体类练习
 * @date 2023-12-01 10:52
 */
public class Wrestle {
    private String name;
    private int blood;

    public Wrestle() {
    }

    public Wrestle(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return blood
     */
    public int getBlood() {
        return blood;
    }

    /**
     * 设置
     *
     * @param blood
     */
    public void setBlood(int blood) {
        this.blood = blood;
    }

    public String toString() {
        return "wrestle{name = " + name + ", blood = " + blood + "}";
    }

    /*打架方法*/
    public void fight(Wrestle wrestle) {
        /*随机数*/
        Random r = new Random();
        /*计算伤害*/
        int i = r.nextInt(20)+1;
        /*剩余伤害*/
        int i1 = wrestle.getBlood() - i;
        i1 = i1 < 0 ? 0 : i1;
        wrestle.setBlood(i1);
        System.out.println(this.getName() + "打了" + wrestle.getName() + "，对" + wrestle.getName() + "造成了" + i + "点伤害，" + wrestle.getName() + "还剩" + i1 + "血量");
    }















    public static void main(String[] args) {
        Wrestle wrestle1 = new Wrestle("张三",100);
        Wrestle wrestle2 = new Wrestle("李四",100);

        while(true){
            wrestle1.fight(wrestle2);
            if(wrestle1.getBlood() == 0){
                System.out.println(wrestle1.getName() + "被打败了");
                break;
            }
            wrestle2.fight(wrestle1);
            if(wrestle2.getBlood() == 0){
                System.out.println(wrestle2.getName() + "被打败了");
                break;
            }
        }
    }
}

